import simplejson
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from glowdrop.utils import adjust_avatar
from glowdrop.message.models import Message
from glowdrop.message import gen_message
from glowdrop.utils import login_required_api

def post_message(request):
    content = request.POST['content']
    src = request.user
    dest = User.objects.get(username=request.POST['dest'])
    if src == dest:
        return HttpResponse(content=_('you could not send message to yourself!'), 
                status=403);
    msg = gen_message(content, src, dest)
    src_avatar = adjust_avatar(msg.src.get_profile().image_link)
    dest_avatar = adjust_avatar(msg.dest.get_profile().image_link)
    print src_avatar, dest_avatar
    ret = [{'id': msg.id, 'content': msg.content, 
            'src': [msg.src.username, src_avatar], 
            'dest': [msg.dest.username, dest_avatar], 
            'pub_date': str(msg.pub_date)}]
    return HttpResponse(simplejson.dumps(ret))

def get_message(request):
    """ get a message with the given id in the request. If there's no such
    message, return http code 404
    @rtype DictinaryType
    @return the message itself
    """
    id = request.GET['id']
    msg = get_object_or_404(Message, pk=id)
    src_avatar = adjust_avatar(msg.src.get_profile().image_link)
    dest_avatar = adjust_avatar(msg.dest.get_profile().image_link)
    ret = [{'content': msg.content, 
            'src': [msg.src.username, src_avatar], 
            'dest': [msg.dest.username, dest_avatar], 
            'pub_date': str(msg.pub_date)}]
    return HttpResponse(simplejson.dumps(ret))

@login_required_api
def message(request):
    """ a pseudo api, when the request method is GET and there's no such message,
    return http code 404
    """
    if request.method == "POST":
        return post_message(request)
    elif request.method == "GET":
        return get_message(request)
    return HttpResponse("invalid operation")

@login_required_api
def message_list(request):
    '''get the message list, with following url arguments:
    * page_no: denoting the page no, if no such argument, start from page 0
    * page_size: mandatory, denoting the number of messages in one page
    * type: if is 'sent', only the sent messages, 'received', only the received 
        messages, if there's no such argument, only received
    '''
    page_no = request.GET.get('page_no', 0)
    page_size = request.GET['page_size']
    type = request.GET.get('type', 'received')
    if type == 'received':
        msg_list = Message.objects.filter(dest=request.user)
    elif type == 'sent':
        msg_list = Message.objects.filter(src=request.user)
    else:
        msg_list = []

    ret = []
    for m in msg_list:
        ret.append({'id': m.id, 'content': m.content, 
            'src': [m.src.username, adjust_avatar(m.src.get_profile().image_link)], 
            'dest': [m.dest.username, adjust_avatar(m.dest.get_profile().image_link)],
            'pub_date': str(m.pub_date)})

    return HttpResponse(simplejson.dumps(ret))
